var app = getApp();
Page( Object.assign({},{
    data:{
        imgs : [],
        pictemp : [],
        pic : [],
        star : 5,
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;

        var oid = options.oid ? options.oid : '';

        app.com.auth(function(user){
            app.com.http('order','POST',{oid:oid,op:'orderinfo'},0,false,'','',function(res){
                if (!res.data.errno && res.data) {
                    self.setData({
                        good : res.data.data.good,
                        oid : oid,
                        inited : true,
                        params : res.data.data.params,
                    });
                    // 关闭下拉刷新
                    wx.stopPullDownRefresh();
                }else{
                    app.com.toast(res.data.message);
                    setTimeout(function(){
                        wx.navigateBack();
                    },500);
                }
            });
        });
        wx.setNavigationBarTitle({
          title:  '评价',
        });
    },
    sure(){
        var self = this;
        var length = self.data.pictemp.length;
        var uparr = self.data.pictemp;
        var temparr = [];
        var flag = 0;
        if( length > 0 ){

            for (let i = 0; i < length; i++) {
                (function(m){
                    
                    self.uptomaster(uparr[m],function(res){

                        temparr.push({id:m,att:res.attachment,show:res.url});
                        flag ++;
                        if( flag == length ) {
                            temparr.sort(function(a,b){
                                return a.id-b.id;
                            });
                            self.setData({
                                'pic' : self.data.pic.concat(temparr)
                            });

                            self.savedata();
                            self.setData({
                                'pictemp' : [],
                            });
                        }

                    });

                })(i)
            }

        }else{
            self.savedata();
        }
    },
    savedata(e){
        var self = this;
        var POST = {
            oid : self.data.oid,
            op : 'subcomment',
            content : self.data.content,
            pic : JSON.stringify( self.data.pic ),
            star : self.data.star,
        };

        if( !POST.content ) {
            app.com.toast('请填写评价内容');return false;
        }

        app.com.http('order','POST',POST,0,true,'','',function(res){
            if (!res.data.errno && res.data) {
                app.com.toast(res.data.message);
                var pages = getCurrentPages();
                var prevPage = pages[pages.length - 2];
                prevPage.setData({
                    'order.status' : 4,
                });

                setTimeout(function(){
                    wx.navigateBack();
                },2000);
                
            }else{
                app.com.toast(res.data.message);
            }
        });
    },
    uploadMult(){
        var self = this;
        wx.chooseImage({
            count: 9, // 默认9
            sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success (res) {
                for(var t in res.tempFilePaths){
                    self.data.pictemp.push( res.tempFilePaths[t] );
                }
                self.setData({
                    pictemp : self.data.pictemp,
                })
            }
        })
    },
    uptomaster(tempurl,fn){
        wx.showLoading({mask:true,title:'上传中'});
        wx.uploadFile({
            url: app.siteInfo.siteroot+'?i='+app.siteInfo.uniacid+'&j=&c=utility&a=file&do=upload&type=image&thumb=0',
            filePath: tempurl,
            name: 'file',
            success: function(res){
                var aaa = JSON.parse( res.data );
                if( fn ) fn( aaa );
            },
            complete : function(){
                wx.hideLoading();
            }
        })
    },
    del(e){
        var id = e.currentTarget.dataset.id;
        this.data.pictemp.splice(id,1);
        this.setData({
            'pictemp' : this.data.pictemp,
        })
    },
    delpic(e){
        var id = e.currentTarget.dataset.id;
        this.data.pic.splice(id,1);
        this.setData({
            'pic' : this.data.pic,
        })
    },
    starfn(e){
        this.setData({
            star : e.detail,
        })
    },
    content(e){
        this.setData({
            content : e.detail.value,
        })
    },
    cancel(){
        wx.navigateBack();
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e);
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    }
    

}))